package com.game.league.web;

import android.graphics.PixelFormat;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.game.base.app.base.BaseActivity;
import com.game.base.utils.device.DeviceUtil;
import com.game.base.x5.web.WebPanel;
import com.game.base.x5.web.util.WebListener;
import com.game.base.x5.web.util.WebLoadStatus;
import com.game.league.R;
import com.game.league.databinding.ActivityWebBinding;

/**
 * 网页容器Activity
 * Created by Su on 2019/5/9.
 */
public class WebActivity extends BaseActivity<ActivityWebBinding> implements View.OnClickListener {
    private String url;
    private String data; // 标签文本
    private WebPanel webPanel;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_web;
    }

    @Override
    protected void initView() {
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        binding.setClick(this);
    }

    @Override
    protected void initData() {
        if (mBundle != null) {
            url = mBundle.getString("url");
            data = mBundle.getString("data");
            binding.setTitle(mBundle.getString("title"));
        }
        webPanel = new WebPanel(this, url, data);
        View contentView = webPanel.getContentView();
        int width = DeviceUtil.getWidth();
        int height = DeviceUtil.getHeight();
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(width, height);
        contentView.setLayoutParams(layoutParams);

        webPanel.setWebListener(new WebListener() {
            @Override
            public void onTitle(String title) {
                binding.setTitle(title);
            }

            @Override
            public void onLoadFinish(WebLoadStatus loadStatus) {
            }
        });
        binding.webContainer.addView(contentView);
    }


    @Override
    public void onBackPressed() {
        if (webPanel != null && webPanel.canGoBack()) {
            webPanel.getWebView().onBackPressed();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (webPanel != null) {
            webPanel.getWebView().onResume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (webPanel != null) {
            webPanel.getWebView().onPause();
        }
    }

    @Override
    protected void onDestroy() {
        if (webPanel != null) webPanel.onDestroy();
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.title_left_iv) {
            finish();
        }
    }
}
